//
//  AppDelegate.m
//  UI_CarHome
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 沈浩. All rights reserved.
//

#import "AppDelegate.h"

#import "SHMyViewController.h"
#import "SHRecommendViewController.h"
#import "SHForumViewController.h"
#import "SHFindCarViewController.h"
#import "SHSaveTool.h"
#import "SHDiscountViewController.h"

#import "SHGuideViewController.h"

#import "SHHandle.h"
#import "XGPush.h"
#import "UMSocial.h"
@interface AppDelegate ()
@property(nonatomic, retain)UIImageView *adImageView;
@property(nonatomic, retain)UITabBarController *tab;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    
    // 1. 注册
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0f) {
        // 系统iOS8.0以上使用
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert categories:nil]];
        
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        // iOS8.0以下
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert];
        
    }
    [XGPush startApp:2200165738 appKey:@"I98RK992GUMS"];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isNight:) name:@"isNight" object:nil];
    self.window.backgroundColor = BACKGROUNDCOLOR;
    [self.window makeKeyAndVisible];
    [_window release];
    

    
    // 判断网络状态
    [self createNetWork];
    // 设置友盟分享
    [UMSocialData setAppKey:@"563c383667e58e4ed7000fba"];
    
    SHRecommendViewController *recommendVC = [[SHRecommendViewController alloc] init];
    UINavigationController *recommendNAVC = [[UINavigationController alloc] initWithRootViewController:recommendVC];
    recommendNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"推荐" image:[UIImage imageNamed:@"tuijian"] tag:1000] autorelease];
    
    SHForumViewController *forumVC = [[SHForumViewController alloc] init];
    UINavigationController *forumNAVC = [[UINavigationController alloc] initWithRootViewController:forumVC];
    forumNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"论坛" image:[UIImage imageNamed:@"luntan"] tag:1001] autorelease];
    
    SHFindCarViewController *findCarVC = [[SHFindCarViewController alloc] init];
    UINavigationController *findCarNAVC = [[UINavigationController alloc] initWithRootViewController:findCarVC];
    findCarNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"找车" image:[UIImage imageNamed:@"zhaoche"] tag:1002] autorelease];
    
    SHDiscountViewController *discountVC = [[SHDiscountViewController alloc] init];
    UINavigationController *discountNAVC = [[UINavigationController alloc] initWithRootViewController:discountVC];
    discountNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"优惠" image:[UIImage imageNamed:@"youhui"] tag:1003] autorelease];
    
    SHMyViewController *myVC = [[SHMyViewController alloc] init];
    UINavigationController *myNAVC = [[UINavigationController alloc] initWithRootViewController:myVC];
    myNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"wo"] tag:1004] autorelease]
    ;
    
    self.tab = [[UITabBarController alloc] init];
    self.tab.viewControllers = @[recommendNAVC, forumNAVC, findCarNAVC, discountNAVC, myNAVC];
    self.tab.tabBar.barTintColor = BACKGROUNDCOLOR;
    self.tab.tabBar.tintColor = [UIColor blueColor];
    
    [SHSaveTool isHaveFolder];

    // 引导页
    void(^block)() = ^{
        self.window.rootViewController = self.tab;
    };
    NSUserDefaults *first = [NSUserDefaults standardUserDefaults];
    if ([first objectForKey:@"first"] == nil) {
        [first setObject:@"have" forKey:@"first"];
        SHGuideViewController *guideView = [[SHGuideViewController alloc] init];
        // 如果第一次启动的话,使用guideView作为根式图
        self.window.rootViewController = guideView;
        [guideView release];
        // 传值,传一个block值
        guideView.block = block;
        
    } else {
        self.window.rootViewController = self.tab;
    }

    
    return YES;
}

// 注册失败返回一个error
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"error ======== %@", error);
}
// 5. 发送通知成功返回通知内容
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"userInfo ====== %@", userInfo);
}

// 2. 注册成功APNS返回Token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // 3. 将获得的Token发给自己的服务器
    [XGPush registerDevice:deviceToken];
    NSLog(@"Token ========= %@", [deviceToken description]);
}
- (void)isNight:(NSNotification *)nsnotification {
    self.window.backgroundColor = BACKGROUNDCOLOR;
    self.tab.tabBar.barTintColor = BACKGROUNDCOLOR;
}

- (void) createNetWork {
    AFNetworkReachabilityManager *manager =  [AFNetworkReachabilityManager sharedManager];
    [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        [self showNetWorkWithStatus:status];
        
    }];
}

- (void) showNetWorkWithStatus:(NSInteger)status {
    NSString *str;
    switch (status) {
        case AFNetworkReachabilityStatusUnknown:
            str = @"未知网络";
            break;
        case AFNetworkReachabilityStatusNotReachable:
            str = @"网络未连接";
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            str = @"网络已连接";
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            str = @"WIFI已连接";
            break;
        default:
            break;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前网络状态" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

- (void)dealloc
{
    [_window release];
    [_adImageView release];
    [super dealloc];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
